package com.anrongtec.zcpt.interfaces.theInferfaces;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.request.PostRequest;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * http请求类
 * cxy
 *
 */

public class HttpUtil {

    /**
     * 通用get请求
     * @param url
     * @param map
     * @param callback
     * @param tag
     */
    public static void get(String url, Map<String,String> map, AbsCallback callback, Object tag){
        OkGo.get(url)
                .tag(tag)
                .params(map)
                .execute(callback);
    }

    /**
     * 通用post请求
     * @param url
     * @param json
     * @param callback
     * @param tag
     */
    public static void post(String url, String json, AbsCallback callback, Object tag){
        OkGo.post(url)
                .tag(tag)
                .upJson(json)
                .execute(callback);
    }
    public static void post(String url, Map<String,String> params, AbsCallback callback, Object tag){
        OkGo.post(url)
                .tag(tag)
                .params(params)
                .execute(callback);
    }

    /**
     * 传文件
     * @param url
     * @param filePaths
     * @param callback
     * @param tag
     */
    public static void postFile(String url, Map<String, String> params, List<String> filePaths, AbsCallback callback, Object tag){

        PostRequest request = OkGo.post(url);
        if (tag != null)
            request.tag(tag);

        for (Map.Entry<String,String> entry: params.entrySet()) {
            request.params(entry.getKey(), entry.getValue());
        }

        for (int i = 0; i < filePaths.size(); i++) {
            request.params("file"+i, new File(filePaths.get(i)));
        }
        request.execute(callback);

    }





    /**
     * 按tag取消请求,   tag一般用activity对象
     * @param tag
     */
    public static void cancel(Object tag){
        OkGo.getInstance().cancelTag(tag);
    }

    /**
     * 取消所有请求
     */
    public static void cancelAll(){
        OkGo.getInstance().cancelAll();
    }



    /**
     * Activity声明周期回调
     *
     * 使用方法: 在application中         //绑定activity生命周期
     * registerActivityLifecycleCallbacks(new HttpUtil.BaseActivityLifecycleCallbacks());
     *
     */
    public static class BaseActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {

        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            cancel(activity.getClass());
        }
    }


}
